package com.jiaoshouMr.ethereum.repository;

import com.jiaoshouMr.ethereum.domain.BlockAccount;
import com.mongodb.client.result.UpdateResult;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

@Repository
public class BlockRepository {
    @Autowired
    private MongoOperations mongoOperations;

    public BlockAccount getAccount(String type ) {
        Query query = query(where("status").is("0").and("type").is(type));
        return mongoOperations.findOne(query, BlockAccount.class, "blockAccount");
    }


    public void updateFirst(String id ) {
        UpdateResult updateResult = mongoOperations.updateFirst(query(where("_id").is(new ObjectId(id))), new Update().set("status", "1"), BlockAccount.class, "blockAccount");
        long matchedCount = updateResult.getMatchedCount();
        System.out.println(matchedCount);

    }

    public void insert(BlockAccount blockAccount ) {
        mongoOperations.insert(blockAccount,"blockAccount");
    }
}
